package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import oa.entity.Application;
import oa.entity.Company;
import oa.entity.Message;
import oa.entity.User;
import oa.paging.Page;
import oa.service.ApplicationService;
import oa.service.CompanyService;
import oa.service.MessageService;
import oa.service.UserService;
import oa.service.impl.ApplicationServiceImpl;
import oa.service.impl.CompanyServiceImpl;
import oa.service.impl.MessageServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class Applicationpag
 */
@WebServlet("/applicationpag")
public class Applicationpag extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Applicationpag() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType

		("text/html;charset=utf-8");
		ApplicationService Application = new

		ApplicationServiceImpl();
		UserService userService = new

		UserServiceImpl();
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		Page<Application> pageInfo = new

		Page<Application>();
		List<Application> list = new

		ArrayList<Application>();
		int counnts = 0;
		String pageNo = request.getParameter

		("pageNo");
		int Stuatus = 0;
		int CompanyId = 0;
		int UserId = 0;
		if (pageNo == null) {
			pageNo = "1";
		}

		try {

			int companyid = (int)

			session.getAttribute("companyid");
			String companyids =

					request.getParameter("companyids");
			String status =

					request.getParameter("status");
			if (status == null) {
				status = "0";
			}
			int userid = (int)

			session.getAttribute("userid");
			pageInfo.setCurrentPage

			(Integer.parseInt(pageNo));
			pageInfo.setPageSize(5);
			// 初始化数据
			if ((companyid == 0 &&

					companyids == null)) {
				Stuatus = Integer.parseInt

				(status);
				CompanyId = companyid;
				list =

						Application.selectApplications(null, companyid,

								pageInfo.getCurrentPage(),

								pageInfo.getPageSize());
				counnts =

						Application.appcount(status, companyid, 0);
				// 根据申请状态查看
			} else if (companyid == 0 && !

			companyids.equals("0")) {
				Stuatus = Integer.parseInt

				(status);
				CompanyId =

						Integer.parseInt(companyids);
				list =

						Application.selectApplications(status, Integer.parseInt

						(companyids), pageInfo.getCurrentPage(),

								pageInfo.getPageSize());
				counnts =

						Application.appcount(status, Integer.parseInt

						(companyids), 0);
				// 公司id都是零的情况下
			} else if (companyid == 0 &&

					companyids.equals("0")) {
				Stuatus = Integer.parseInt

				(status);
				CompanyId =

						Integer.parseInt(companyids);
				list =

						Application.selectApplications(status, Integer.parseInt

						(companyids), pageInfo.getCurrentPage(),

								pageInfo.getPageSize());
				counnts =

						Application.appcount(status, Integer.parseInt

						(companyids), 0);
				// 当前登录的用户有公司id
			} else if (companyid != 0 &&

					companyids == null && userid == 0) {
				Stuatus = Integer.parseInt

				(status);
				CompanyId = companyid;

				list =

						Application.selectApplications(status, companyid,

								pageInfo.getCurrentPage(),

								pageInfo.getPageSize());
				counnts =

						Application.appcount(status, companyid, 0);
				/// 判断当前登录的员工发起的申请
			} else if (companyid != 0 && userid

			!= 0) {
				UserId = userid;
				list =

						Application.selectApplicationusercompany(userid,

								pageInfo.getCurrentPage(), pageInfo.getPageSize());
				counnts =

						Application.appcount(null, 0, userid);
			}

			pageInfo.setTotalRow(counnts);
			pageInfo.setPageList(list);
			List<User> userList =

					userService.selectAllUser(null, 0, 0);
			request.getSession().setAttribute

			("userList", userList);
			request.getSession().setAttribute

			("ApplicationList", pageInfo);
			request.getSession().setAttribute

			("Stuatus", Stuatus);
			request.getSession().setAttribute

			("UserId", UserId);
			request.getSession().setAttribute

			("CompanyId", CompanyId);
			request.getRequestDispatcher

			("application_list.jsp").forward(request, response);

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
